<template>
  <div class="content">
    <el-space class="search">
      <el-input
        v-model="title"
        placeholder="请输入题目关键字"
        style="width: 200px"
      />
      <el-input
        v-model="content"
        placeholder="请输入内容关键字"
        style="width: 200px"
      />
      <el-input
        v-model="decription"
        placeholder="请输入描述关键字"
        style="width: 200px"
      />
      <el-button @click="search">搜索</el-button>
    </el-space>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="title" label="标题">
        <template #default="scope">
          <el-popover
            effect="light"
            trigger="hover"
            placement="top"
            width="auto"
          >
            <template #default>
              <div>name: {{ scope.row.title }}</div>
            </template>
            <template #reference>
              <el-tag>{{ scope.row.title }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" :formatter="formatterText" />
      <el-table-column
        prop="description"
        label="描述"
        :formatter="formatterText"
      />
      <el-table-column
        prop="categories"
        label="分类"
        :formatter="formatterText"
      >
        <template #default="scope">
          <div
            v-for="(category, index) in scope.row.categories"
            :key="index"
            style="display: inline-block"
          >
            <el-tag style="marginleft: 10px">{{ category.title }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="推荐">
        <template #default="scope">
          <el-switch
            v-model="scope.row.recommend"
            @change="recommendBlog(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="created" label="修改时间" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
          <el-button size="small" @click="check(scope.$index, scope.row)"
            >图片</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="sizes, prev, pager, next, jumper"
      :total="total"
      :page-size="pageSize"
      v-model:current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      @size-change="sizeChange"
      @current-change="currentChange"
      class="pagination"
    />
    <el-dialog v-model="dialogVisible" lock-scroll="false" style="width: 40%">
      <el-image :src="url" fit="cover" :preview-src-list="list" class="img" />
    </el-dialog>
  </div>
</template>

<script>
import {
  getAllBlogs,
  searchBlogs,
  deleteBlog,
  openRecommend,
  closeRecommend,
} from "@/api/blog";
import { ElMessage } from "element-plus";
import dayjs from "dayjs";
export default {
  data() {
    return {
      url: "xxx",
      list: [],
      dialogVisible: false,
      tableData: [],
      total: 10,
      pageSize: 5,
      currentPage: 1,
      userInfo: JSON.parse(sessionStorage.getItem("userInfo")),
      title: "",
      content: "",
      decription: "",
    };
  },
  created() {
    this.getData(this.currentPage, this.pageSize, this.userInfo.id);
  },
  methods: {
    getData(currentPage, size, id) {
      getAllBlogs(currentPage, size, id).then((res) => {
        if (res.code == 200) {
          this.total = res.data.total;
          const oldBlogs = res.data.blogDtos;
          const blogs = [];
          for (let i = 0; i < oldBlogs.length; i++) {
            const blog = oldBlogs[i].blog;
            blog.created = dayjs(blog.created).format("YYYY-MM-DD HH:mm:ss");
            blog.recommend = blog.recommend === 1 ? true : false;
            blog.categories = oldBlogs[i].categories;
            blogs.push(blog);
          }
          this.tableData = blogs;
        }
      });
    },
    recommendBlog(row) {
      console.log("row", row);
      const isRecommend = row.recommend;
      const id = row.id;
      if (isRecommend) {
        openRecommend(id).then((res) => {
          if (res.code === 200) {
            ElMessage.success("开启推荐文章成功");
          } else {
            ElMessage.error("开启推荐文章失败");
          }
        });
      } else {
        closeRecommend(id).then((res) => {
          if (res.code === 200) {
            ElMessage.success("关闭推荐文章成功");
          } else {
            ElMessage.error("关闭推荐文章失败");
          }
        });
      }
    },
    handleEdit(scope, row) {
      this.$store.commit("SET_ISEDITBLOG", true);
      this.$store.commit("SET_EDITBLOGINFO", row);
      this.$router.push("/edit");
    },
    handleDelete(scope, row) {
      const id = row.id;
      deleteBlog(id).then((res) => {
        if (res.code == 200) {
          this.getData(this.currentPage, this.pageSize, this.userInfo.id);
          ElMessage.success("删除文章成功");
        } else {
          ElMessage.error(res.msg);
        }
      });
    },
    check(scope, row) {
      this.dialogVisible = true;
      const url = row.img;
      this.url = url;
      this.list.push(url);
    },
    sizeChange(size) {
      this.getData(this.currentPage, size, this.userInfo.id);
    },
    currentChange(current) {
      this.getData(current, this.pageSize, this.userInfo.id);
    },
    search() {
      searchBlogs(
        this.title,
        this.decription,
        this.content,
        this.contentPage,
        this.pageSize,
        this.userInfo.id
      ).then((res) => {
        if (res.code == 200) {
          const oldBlogs = res.data.blogDtos;
          console.log(oldBlogs);
          const blogs = [];
          for (let i = 0; i < oldBlogs.length; i++) {
            const blog = oldBlogs[i].blog;
            blog.created = dayjs(blog.created).format("YYYY-MM-DD HH:mm:ss");
            blog.categories = oldBlogs[i].categories;
            blog.recommend = blog.recommend === 1 ? true : false;
            blogs.push(blog);
          }
          this.tableData = blogs;
        }
      });
    },
    formatterText(row, column, cellValue) {
      const name = cellValue.slice(0, 10);
      return name;
    },
  },
};
</script>

<style lang="less" scoped>
.content {
  margin-top: 10px;
  .search {
    display: flex;
    position: relative;
    flex-direction: row;
    justify-content: center;
    button {
      margin-right: 10px;
    }
  }
  .pagination {
    margin-top: 10px;
    display: flex;
    justify-content: flex-end;
    align-content: space-around;
    margin-right: 100px;
  }
  // .img {
  //   width: 20%;
  // }
}
</style>
